;;; org-mode

;;; Code:
(global-set-key (kbd "C-c l") #'org-store-link)
(global-set-key (kbd "C-c a") #'org-agenda)
(global-set-key (kbd "C-c c") #'org-capture)

(setq org-agenda-files (list "~/GTD/inbox.org"
			     "~/GTD/someday.org"
			     "~/GTD/tickler.org"
			     "~/GTD/gtd.org"))

;;; org-capture 指定文件位置(c-c c)
(setq org-capture-templates '(("t" "Todo [inbox]" entry
                               (file+headline "~/GTD/inbox.org" "Tasks")
                               "* %i%?")
                              ("T" "Tickler" entry
                               (file+headline "~/GTD/tickler.org" "Tickler")
                               "* %i%? \n %U")))

;;; refile targets(c-c c-w)
(setq org-refile-targets '(("~/GTD/gtd.org" :maxlevel . 2)
                           ("~/GTD/someday.org" :level . 1)
                           ("~/GTD/tickler.org" :maxlevel . 1)))

;;; TODO completed
(setq org-todo-keywords '((sequence "TODO(t)" "WAITING(w)" "|" "DONE(d)" "CANCELLED(c)")))

;;; org-agenda(c-c c-a)
(setq org-agenda-custom-commands 
      '(("o" "At the office" tags-todo "@OFFICE"
         ((org-agenda-overriding-header "OFFICE")
	  (org-agenda-skip-function #'my-org-agenda-skip-all-siblings-but-first)))))
;; 只搜索第一个代办事项
(defun my-org-agenda-skip-all-siblings-but-first ()
  "Skip all but the first non-done entry."
  (let (should-skip-entry)
    (unless (org-current-is-todo)
      (setq should-skip-entry t))
    (save-excursion
      (while (and (not should-skip-entry) (org-goto-sibling t))
        (when (org-current-is-todo)
          (setq should-skip-entry t))))
    (when should-skip-entry
      (or (outline-next-heading)
          (goto-char (point-max))))))
(defun org-current-is-todo ()
  (string= "TODO" (org-get-todo-state)))

;;; 美化

;; - to •
 (font-lock-add-keywords 'org-mode
                            '(("^ +\\([-*]\\) "
                               (0 (prog1 () (compose-region (match-beginning 1) (match-end 1) "•"))))))

;; proportional font
  (let* ((variable-tuple
          (cond ((x-list-fonts "ETBembo")         '(:font "ETBembo"))
                ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro"))
                ((x-list-fonts "Lucida Grande")   '(:font "Lucida Grande"))
                ((x-list-fonts "Verdana")         '(:font "Verdana"))
                ((x-family-fonts "Sans Serif")    '(:family "Sans Serif"))
                (nil (warn "Cannot find a Sans Serif Font.  Install Source Sans Pro."))))
         (base-font-color     (face-foreground 'default nil 'default))
         (headline           `(:inherit default :weight bold :foreground ,base-font-color)))

    (custom-theme-set-faces
     'user
     `(org-level-8 ((t (,@headline ,@variable-tuple))))
     `(org-level-7 ((t (,@headline ,@variable-tuple))))
     `(org-level-6 ((t (,@headline ,@variable-tuple))))
     `(org-level-5 ((t (,@headline ,@variable-tuple))))
     `(org-level-4 ((t (,@headline ,@variable-tuple :height 1.05))))
     `(org-level-3 ((t (,@headline ,@variable-tuple :height 1.10))))
     `(org-level-2 ((t (,@headline ,@variable-tuple :height 1.15))))
     `(org-level-1 ((t (,@headline ,@variable-tuple :height 1.20))))
     `(org-document-title ((t (,@headline ,@variable-tuple :height 1.30 :underline nil))))))

(custom-theme-set-faces
 'user
 '(variable-pitch ((t (:family "ETBembo" :height 140 :weight thin))))
 '(fixed-pitch ((t ( :family "Fira Code Retina" :height 140)))))

(custom-theme-set-faces
 'user
 '(org-block ((t (:inherit fixed-pitch))))
 '(org-code ((t (:inherit (shadow fixed-pitch)))))
 '(org-document-info ((t (:foreground "dark orange"))))
 '(org-document-info-keyword ((t (:inherit (shadow fixed-pitch)))))
 '(org-indent ((t (:inherit (org-hide fixed-pitch)))))
 '(org-link ((t (:foreground "royal blue" :underline t))))
 '(org-meta-line ((t (:inherit (font-lock-comment-face fixed-pitch)))))
 '(org-property-value ((t (:inherit fixed-pitch))) t)
 '(org-special-keyword ((t (:inherit (font-lock-comment-face fixed-pitch)))))
 '(org-table ((t (:inherit fixed-pitch :foreground "#83a598"))))
 '(org-tag ((t (:inherit (shadow fixed-pitch) :weight bold :height 0.9))))
 '(org-verbatim ((t (:inherit (shadow fixed-pitch))))))

(add-hook 'org-mode-hook 'visual-line-mode)

(use-package org-bullets
  :ensure t
  :hook(org-mode . org-bullets-mode)
  :config
  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1)))) 

;;; evil-org
(use-package evil-org
  :after org
  :hook (org-mode . evil-org-mode)
  :config
  (require 'evil-org-agenda)
  (evil-org-agenda-set-keys))


;;; .emacs.d/lisp
(defun gtd ()
  "快速进入~/GTD/文件夹."
  (interactive)
  (counsel-find-file "~/GTD/"))




(provide 'org-mode)
